package com.liu;

import java.util.concurrent.Exchanger;

public class ExChagerExample {
    public static void main(String[] args) {
        //Exchanger用于线程间的信息交换
        Exchanger<String> stringExchanger = new Exchanger<>();
        Thread thread = new Thread(() -> {
            String producerMessage = "Hello producer World!";
            try {
                stringExchanger.exchange(producerMessage);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        });
        Thread thread1 = new Thread(() -> {
            String consumerMessage = "Hello consumer World!";
            try {
                String returnMessage = stringExchanger.exchange(consumerMessage);
                System.out.println(returnMessage);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        thread.start();
        thread1.start();
    }
}
